//
//  QLTitleButton.swift
//  MQLWeibo
//
//  Created by MQL-IT on 2017/3/16.
//  Copyright © 2017年 MQL-IT. All rights reserved.
//

import UIKit

class QLTitleButton: UIButton {
    /// 重载构造函数
    /// title为nil 显示首页, 不为nil显示title和图标
    
    init(title: String?) {
        super.init(frame: CGRect());
        //1. 判断title是否为nil
        if title == nil {
            setTitle("首页", for: [])
        } else {
            setTitle(title! + " ", for: [])
            // 图标
            setImage(UIImage(named: "navigationbar_arrow_up"), for: .normal)
            setImage(UIImage(named: "navigationbar_arrow_down"), for: .selected)
        }
        
        //2.设置字体和颜色
        titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        setTitleColor(UIColor.darkGray, for: .normal)
        
        //3.大小
        sizeToFit()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 重新布局子视图
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //判断titleLabel和imageView是否同时存在
        guard let titleLabel = titleLabel, let imageView = imageView else { return }
        
        printLog(message: "\(titleLabel)===\(imageView)")
        
        titleEdgeInsets = UIEdgeInsetsMake(0, -2 * imageView.bounds.size.width, 0, 0)
        imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -2 * titleLabel.bounds.size.width)
        
        //将Label的 x 向左移动 imageView的宽度, !!!!!!有问题
//        titleLabel.frame = titleLabel.frame.offsetBy(dx: -imageView.bounds.size.width, dy: 0)
////        //将imageVie的 x 向右移动 label的宽度
//        imageView.frame = imageView.frame.offsetBy(dx: titleLabel.bounds.size.width, dy: 0)
        

        
    }
    

}
